package logicaTemporal;


import jade.core.Agent;
import jade.core.behaviours.*;

import java.io.*;

import jade.lang.acl.ACLMessage;

import java.util.Iterator;
import java.util.StringTokenizer;

import java.lang.String;

import jess.*;

public class Controlador extends Agent{
	
	protected void setup(){
		
		addBehaviour(new controlJess());
		
	}
	
	
	class controlJess extends OneShotBehaviour {
    
    	
    	
    	public void action() {
    		    	
    	
	    	Rete jess = new Rete();
	    
	    	try{
		    	FileReader file = new FileReader("src/logicaTemporal/sentenciasLogicas.clp");
		    	try {
		    	     Jesp parser = new Jesp(file, jess);
				     parser.parse(false);
				 } catch (jess.JessException je) {
		                 	je.printStackTrace();
		                }
				  finally {
				     file.close();
				 }
				 }catch(IOException e) {
	             	System.err.println("Error!! No se ha podido acceder al archivo.");
             }
             
           
	    	try {
        		jess.reset();
        		jess.batch("src/logicaTemporal/sentenciasLogicas.clp");
        		
        		
				
	            Iterator it = jess.listFacts();
				while(it.hasNext()){
					System.out.println(it.next());	
				}
				
		    } catch (JessException ex) {
		        System.err.println(ex);
		    }
		    
			while(true){
				block(2500);
				
				try{
					
					ACLMessage msg=myAgent.receive();
	            	if(msg!=null) {
			        	String contenido=msg.getContent();
			        	
			        	
			            if (contenido.equals("virus encontrado")){ 
		        			jess.eval("(virus)");
		        		}
			            else if (contenido.equals("correo importante")){ 
		        			jess.eval("(importante)");
		        		}
			            else if (contenido.equals("correo spam")){ 
		        			jess.eval("(spam)");
		        		}
			        	
			        	jess.run();
			        				        	
			        }
		        } catch (JessException ex) {
		        System.err.println(ex);
		    }	
			}
		
	
    	}
    	
    }

}